define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('create_CanvasLayer', function (sourceId, layerId, name, zIndex) {
        var layer = new egis.carto.CanvasLayer({
            "id": layerId,
            "name": name,
            "zIndex": zIndex
        });
        Window.container[sourceId] = layer;
    })

    dsBridge.register('CanvasLayer_add', function (sourceId, elementId, content) {
        var elementLayer = Window.container[sourceId];
        var target = Window.container[elementId];
        if(!target){
            target = Window.deserializer.createJsInstance(JSON.parse(content));
            target.reset();
            Window.container[elementId] = target;
        }
        elementLayer.add(target);
    })

    dsBridge.register('CanvasLayer_addGElement', function (sourceId, gElementId,content) {
        var canvasLayer = Window.container[sourceId];
        var target = Window.container[gElementId];
        var gElement = Window.deserializer.createJsInstance(JSON.parse(content));
        target.fields = gElement.fields;
        target.properties = gElement.properties;
        canvasLayer.addGElement(target);
    })

    dsBridge.register('CanvasLayer_addElementCollection', function (sourceId, elements) {
        var elementCollection = Window.deserializer.createJsInstance(JSON.parse(elements));
        elementCollection.reset();
        var index = elementCollection.getIdentityIndex();
        for(var i = 0, len = elementCollection.size(); i < len;++i){
            var element = elementCollection.get(i);
            var gElement = Window.container[element.id];
            var property = element.getPropertyByIndex(index);
            gElement.setPropertyByIndex(index, property);
        }
        elementLayer.addElementCollection(elementCollection);
    })

    dsBridge.register('CanvasLayer_remove', function (sourceId, elementId) {
        var cavansLayer = Window.container[sourceId];
        var target = Window.container[elementId];
        cavansLayer.remove(target);
    })

    dsBridge.register('CanvasLayer_showElement', function (sourceId, elementId, visible, content) {
        var elementLayer = Window.container[sourceId];
        var element = Window.container[elementId];
        if(!element){
            element = Window.deserializer.createJsInstance(JSON.parse(content));
            element.reset();
            Window.container[elementId] = element;
        }
        elementLayer.showElement(element,visible);
    })

    dsBridge.register('CanvasLayer_update', function (sourceId, targetId, isUpdateStyle, content) {
        var elements = Window.deserializer.createJsInstance(JSON.parse(content));
                var source = Window.container[sourceId];
                var target = Window.container[targetId];
                if(!!target){
                    for(var i = 0; i < target.getCount(); i++){
                        var element = target.getItem(i);
                        var element1 = elements.getItem(i);
                        if(!isUpdateStyle){
                            element.geometry = element1.geometry;
                        }
                        else {
                            element.symbol = element1.symbol;
                        }
                    }
                }
                source.update(target, isUpdateStyle);

    })

    dsBridge.register('CanvasLayer_setZIndex', function (sourceId, zIndex) {
        var source = Window.container[sourceId];
        source.setZIndex(zIndex);
    })

    dsBridge.register('CanvasLayer_hitTest', function (sourceId, screenX, screenY) {
        var source = Window.container[sourceId];
        return source.hitTest(screenX, screenY);
    })

    dsBridge.register('CanvasLayer_hitTestList', function (sourceId, screenX, screenY) {
        var source = Window.container[sourceId];
        return source.hitTestList(screenX, screenY);
    })

    dsBridge.register('CanvasLayer_select', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.select(graphic);
    })

    dsBridge.register('CanvasLayer_unSelect', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.unSelect(graphic);
    })

    /*dsBridge.register('CanvasLayer_clear', function (sourceId, opt_fast) {
        var source = Window.container[sourceId];
        source.clear(true);
    })

    dsBridge.register('CanvasLayer_setVisible', function (sourceId, visible) {
        var source = Window.container[sourceId];
        source.setVisible(visible);
    })

    dsBridge.register('CanvasLayer_setOpacity', function (sourceId, opacity) {
        var source = Window.container[sourceId];
        source.setOpacity(opacity);
    })

    dsBridge.register('CanvasLayer_setMinZoom', function (sourceId, minZoom) {
        var source = Window.container[sourceId];
        source.setMinZoom(minZoom);
    })

    dsBridge.register('CanvasLayer_setMaxZoom', function (sourceId, maxZoom) {
        var source = Window.container[sourceId];
        source.setMaxZoom(maxZoom);
    })*/


    dsBridge.register('CanvasLayer_selectEdit', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.add(graphic);
    })

    dsBridge.register('CanvasLayer_unSelectEdit', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.add(graphic);
    })

    dsBridge.register('CanvasLayer_bringTop', function (sourceId, elementId) {
        var elementLayer = Window.container[sourceId];
        var element = Window.container[elementId];
        elementLayer.bringTop(element);
    })

    dsBridge.register('CanvasLayer_sendBottom', function (sourceId, elementId) {
        var elementLayer = Window.container[sourceId];
        var element = Window.container[elementId];
        elementLayer.sendBottom(element);
    })
})